package org.piggy.server.service;

import org.piggy.api.server.TestService;
import org.piggy.common.anno.Remote;
import org.piggy.common.entity.Result;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Remote
public class TestServiceImpl implements TestService {

    @Override
    public Result test1(String userName, List<String> list, int i) {
        Result resultPiggy = new Result();
        resultPiggy.setI(i);
        resultPiggy.setList(list);
        resultPiggy.setString(userName);
        return resultPiggy;
    }

    public static final Map<String,Integer> products = new ConcurrentHashMap<>();


    static{
        products.put("1", 10);
    }

    @Override
    public Object test2(String productId) {
        //此处需改成redis分布式锁，由于资源有限，只采用了jvm锁
        synchronized (products){
            //此处库存也放在本地缓存，正常秒杀应该放入redis中
            Integer productVal = products.get(productId);
            if(productVal>0){
                products.put(productId, productVal-1);
                return "购买成功";
            }
        }
        return "购买失败";
    }
}
